package com.yokit.c2c.token;

import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author fengzeng
 * @create 2021/6/13 20:14
 */
public class  TokenProcessor {

  /**
   * 生成Token
   * @return
   */
  public static String makeToken() {
    String token = System.currentTimeMillis() + new Random().nextInt(999999999) + "";
    try {
      //数据指纹   128位长   16个字节  md5
      MessageDigest md = MessageDigest.getInstance("md5");
      byte md5[] = md.digest(token.getBytes(StandardCharsets.UTF_8));
      //base64编码--任意二进制编码明文字符
      BASE64Encoder encoder = new BASE64Encoder();
      return encoder.encode(md5);
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }
}
